import Vue from 'vue'
import Vuex from 'vuex'
import { router } from '../../router'
import { getSession, setSession } from '@/utils/session'
import { openApiSysBaseInfoQuery } from '@/api/openApi'
import { openApiSysAddressQueryFriendlyLink } from '@/api/openApi'
Vue.use(Vuex)

const state = {
  webinfo: JSON.parse(getSession('webinfo')),
  friendLink: JSON.parse(getSession('friendLink')),
  buttons: getSession('buttons') ? getSession('buttons').split(',') : '',
  _event: '',
}

const getters = {
  webName(state) {
    if (!state.webinfo) return
    let infoArr = state.webinfo.filter((v) => v.code == 'webName')
    if (infoArr.length == 0) return
    return infoArr[0].value
  },
  copyright(state) {
    if (!state.webinfo) return
    let infoArr = state.webinfo.filter((v) => v.code == 'copyright')
    if (infoArr.length == 0) return
    return infoArr[0].value
  },
  serveInfo(state) {
    if (!state.webinfo) return
    let infoArr = state.webinfo.filter((v) => v.code == 'serveInfo')
    if (infoArr.length == 0) return
    return infoArr[0].value
  },
  logo(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'logo')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  logoIn(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'logo-in')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  loginBg(state) {
    if (!state.webinfo) return
    let loginInfo = state.webinfo.filter((v) => v.code == 'LOGIN_BG')
    if (!loginInfo.length || !loginInfo[0].hasOwnProperty('sysAttachment'))
      return
    return loginInfo[0].sysAttachment.httpUrl
  },
  webFavicon(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'webFavicon')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  headPic(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'headPic')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  defaultCourseImg(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'defaultCourseImg')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  defaultSpecialtyImg(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'defaultSpecialtyImg')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  defaultArmImg(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'defaultArmImg')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  defaultKnowledgeImg(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'defaultKnowledgeImg')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  defaultEducationImg(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'defaultEducationImg')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  homeBg01(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'homeBg01')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  homeBg02(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'homeBg02')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  adminBg01(state) {
    if (!state.webinfo) return
    let bgArr = state.webinfo.filter((v) => v.code == 'adminBg01')
    if (bgArr.length == 0) return
    return bgArr[0].sysAttachment.httpUrl
  },
  buttons(state) {
    if (!state.buttons) return
    return state.buttons
  },
  friendLink(state) {
    if (!state.friendLink) return
    return state.friendLink
  },
  windowWidth() {
    return document.documentElement.clientWidth || document.body.clientWidth
  },
  windowHeight() {
    return document.documentElement.clientHeight || document.body.clientHeight
  },
  defaultImg() {
    return require('@/assets/no_pic.png')
  },
}

const actions = {
  setwebinfo({ commit }) {
    return new Promise((resolve, reject) => {
      let webinfo = JSON.parse(getSession('webinfo'))
      if (webinfo) {
        resolve(webinfo)
      } else {
        openApiSysBaseInfoQuery({ page: 1, limit: 99 })
          .then((res) => {
            let { data } = res.data
            commit('SETWEBINFO', data)
            let title = data.find((v) => v.code == 'webName').value
            document.title = title || '红色资源管理平台'
            let $favicon = document.querySelector('link[rel="icon"]')
            let webFaviconInfo = data.find((v) => v.code == 'webFavicon')
            if (
              webFaviconInfo &&
              webFaviconInfo.sysAttachment  
             ) {
              let webFavicon = webFaviconInfo.sysAttachment.httpUrl
              $favicon.href = webFavicon
            }
            resolve(data)
          })
          .catch((error) => {
            router.replace({ name: 'maintained' })
            reject(error)
          })
      }
    })
  },
  //友情链接
  friendLink({ commit }) {
    return new Promise((resolve, reject) => {
      // let friendLink = JSON.parse(getSession("friendLink"));
      let friendLink = undefined
      if (friendLink) {
        resolve(friendLink)
      } else {
        openApiSysAddressQueryFriendlyLink()
          .then((res) => {
            let { data } = res.data
            commit('SETFRIENDLINK', data)
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
}

const mutations = {
  SETWEBINFO: (state, d) => {
    if (!d) return
    setSession('webinfo', JSON.stringify(d))
    state.webinfo = d
  },
  SETFRIENDLINK: (state, d) => {
    if (!d) return
    setSession('friendLink', JSON.stringify(d))
    state.friendLink = d
  },
  //设置当前事件对象（购物车动画）
  SETCURRENTEVENT: (state, d) => {
    if (!d) return
    state._event = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
